package com.example.douyin;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.douyin.adapter.MyFragAdapter;
import com.example.douyin.cons.ConsFragment;
import com.example.douyin.databinding.ActivityDouBinding;
import com.example.douyin.friends.FriendsFragment;
import com.example.douyin.home.HomeFragment;
import com.example.douyin.mine.MineFragment;
import com.example.lib_base_mvvm.baseMVVM.BaseView.BaseViewActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/app/DouActivity")
public class DouActivity extends BaseViewActivity<ActivityDouBinding> {
  List<Fragment> list = new ArrayList<>();
  MyFragAdapter myFragAdapter;


    @Override
    public int bindLayout() {
        return R.layout.activity_dou;
    }

    @Override
    public void initView() {

        requestPermissions(new String[]{
                "android.permission.VIBRATE",
                "android.permission.RECORD_AUDIO" ,
                "android.permission.CAMERA",
                "android.permission.ACCESS_NETWORK_STATE",
                "android.permission.WRITE_EXTERNAL_STORAGE" ,
                "android.permission.ACCESS_FINE_LOCATION",
                "android.permission.GET_TASKS" ,
                "android.permission.ACCESS_WIFI_STATE" ,
                "android.permission.CHANGE_WIFI_STATE" ,
                "android.permission.WAKE_LOCK",
                "android.permission.MODIFY_AUDIO_SETTINGS",
                "android.permission.READ_PHONE_STATE",
                "android.permission.RECEIVE_BOOT_COMPLETED",
                "android.permission.FOREGROUND_SERVICE",
                "android.permission.ACCESS_COARSE_LOCATION"

        },100);



      list.add(new HomeFragment());
      list.add(new ConsFragment());
      list.add(new FriendsFragment());
      list.add(new MineFragment());

      myFragAdapter = new MyFragAdapter(this,list);
      dataBinding.vp.setAdapter(myFragAdapter);

      //上下联动
        dataBinding.vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position){
                    case 0:
                        dataBinding.bottom.getMenu().getItem(0).setChecked(true);
                        break;
                    case 1:
                        dataBinding.bottom.getMenu().getItem(1).setChecked(true);
                        break;
                    case 2:
                        dataBinding.bottom.getMenu().getItem(3).setChecked(true);
                        break;
                    case 3:
                        dataBinding.bottom.getMenu().getItem(4).setChecked(true);
                        break;
                }
            }
        });

        dataBinding.bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        dataBinding.vp.setCurrentItem(0);
                        break;
                    case R.id.cons:
                        dataBinding.vp.setCurrentItem(1);
                        break;
                    case R.id.live:
                        ARouter.getInstance().build("/app/LiveActivity").navigation();
                        break;
                    case R.id.friend:
                        dataBinding.vp.setCurrentItem(2);
                        break;
                    case R.id.mine:
                        dataBinding.vp.setCurrentItem(3);
                        break;

                }
                return true;
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            fragment.onActivityResult(requestCode,resultCode,data);
        }

    }
}